﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for AcountDAL
/// </summary>
public class AcountDAL
{
    string str = ConfigurationManager.ConnectionStrings["SmartAccessories"].ToString();
    public AcountDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public DataTable LoadAcount()
    {
        
        SqlConnection cnn = new SqlConnection(str);
        cnn.Open();
        SqlDataAdapter da = new SqlDataAdapter("sp_LoadAcount", cnn);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet ds = new DataSet();
        DataTable dt;
        try
        {
            da.Fill(ds);
            dt = ds.Tables[0];
            return dt;

        }
        catch
        {
            throw;
        }
        finally
        {
            ds.Dispose();
            da.Dispose();
            cnn.Close();
            cnn.Dispose();
        }
    }
    public int insertAcount(Acount a)
    {
        SqlConnection cnn = new SqlConnection(str);
        cnn.Open();
        SqlCommand cmd = new SqlCommand("sp_INSERT_ACOUNT", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            cmd.Parameters.AddWithValue("@username", a.Username);
            cmd.Parameters.AddWithValue("@pass", a.Pass);
            cmd.Parameters.AddWithValue("@Identification", a.Identification);
            cmd.Parameters.AddWithValue("@date_Registry", a.Dateregistry);
            cmd.Parameters.AddWithValue("@AcountType", a.Acounttype);

            return cmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            cnn.Close();
            cnn.Dispose();
        }
    }
    public int updateAcount(Acount a)
    {
        SqlConnection cnn = new SqlConnection(str);
        cnn.Open();
        SqlCommand cmd = new SqlCommand("sp_UPDATE_ACOUNT", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            cmd.Parameters.AddWithValue("@username", a.Username);
            cmd.Parameters.AddWithValue("@pass", a.Pass);
            cmd.Parameters.AddWithValue("@Identification", a.Identification);
            cmd.Parameters.AddWithValue("@date_Registry", a.Dateregistry);
            cmd.Parameters.AddWithValue("@AcountType", a.Acounttype);

            return cmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            cnn.Close();
            cnn.Dispose();
        }
    }
    public int deleteAcount(Acount a)
    {
        SqlConnection cnn = new SqlConnection(str);
        cnn.Open();
        SqlCommand cmd = new SqlCommand("sp_DELETE_ACOUNT", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            cmd.Parameters.AddWithValue("@username", a.Username);
            return cmd.ExecuteNonQuery();
        }
        catch
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            cnn.Close();
            cnn.Dispose();
        }
    }
}